/**
 *
 */
package cn.com.rexen.rplus.sa.controller;

import cn.com.rexen.rplus.controller.CURDControllerSupport;
import cn.com.rexen.rplus.core.criteria.Criteria;
import cn.com.rexen.rplus.core.criteria.Order;
import cn.com.rexen.rplus.core.criteria.Page;
import cn.com.rexen.rplus.core.service.IService;
import cn.com.rexen.rplus.sa.entity.Operate;
import cn.com.rexen.rplus.sa.model.OperateVO;
import cn.com.rexen.rplus.sa.service.OperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghai
 */
@Controller
@RequestMapping(value = OperateController.REQUEST_PATH)
public class OperateController extends CURDControllerSupport<Operate, OperateVO> {
    /**
     * 组织机构请求路径.
     */
    public static final String REQUEST_PATH = "operate";


    @Autowired
    private OperateService operateService;


    /* (non-Javadoc)
     * @see cn.com.rexen.rplus.controller.CURDControllerSupport#getEntity()
     */
    @Override
    public Operate getEntity() {
        return new Operate();
    }

    /* (non-Javadoc)
     * @see cn.com.rexen.rplus.controller.CURDControllerSupport#getEntityService()
     */
    @Override
    protected IService getEntityService() {
        // TODO Auto-generated method stub
        return operateService;
    }

    @RequestMapping(value = "/load", method = RequestMethod.GET)
    @ResponseBody
    public ModelMap load(HttpServletRequest request, HttpServletResponse response) {
        ModelMap mm = new ModelMap();
        Criteria criteria = operateService.getCriteria(Operate.class);
        criteria.addOrder(Order.asc("id"));
        List<Operate> find = operateService.find(criteria);
        List<OperateVO> operateVOList = new ArrayList<OperateVO>();
        for (Operate operate : find) {
            OperateVO ov = new OperateVO(operate.getId(), true, operate.getName());
            operateVOList.add(ov);
        }
        mm.addAttribute("data", operateVOList);
        return mm;
    }

    /* (non-Javadoc)
     * @see cn.com.rexen.rplus.controller.CURDControllerSupport#doList(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public Page doList(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        return null;
    }


}
